package com.binyong.interceptor;

import com.binyong.annotation.NoLogin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 公共拦截方法
 * @author binyong
 * @date 2025-27-21
 */
@Slf4j
public class NoLoginInterceptor implements HandlerInterceptor {


    /**
     * 登录拦截 可用于校验客户信息
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler chosen handler to execute, for type and/or instance evaluation
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //不拦截方法
        if(handler instanceof HandlerMethod){
            HandlerMethod method = (HandlerMethod)handler;
            if (method.getBeanType().isAnnotationPresent(NoLogin.class)) {
                return true;
            }
            if (method.getMethodAnnotation(NoLogin.class) != null) {
                return true;
            }
        }

        //此处可增加校验信息

        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
